package com.js.common.handler;

import com.js.common.exception.auth.LoginException;
import com.js.common.response.BaseResponse;
import com.js.core.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ygl
 * @create 2018-10-26
 * @DESCRIPTION
 * 全局异常拦截处理器
 **/
@Slf4j
//@RestControllerAdvice("com.js")
@ControllerAdvice("com.js")
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
        log.error(ex.getMessage(),ex);
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        log.error(ex.getMessage(),ex);
        return new BaseResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }

//    @ExceptionHandler(ClientTokenException.class)
//    public BaseResponse clientTokenExceptionHandler(HttpServletResponse response, ClientTokenException ex) {
//        response.setStatus(HttpStatus.FORBIDDEN.value());
//        log.error(ex.getMessage(),ex);
//        return new BaseResponse(ex.getStatus(), ex.getMessage());
//    }

    @ExceptionHandler(LoginException.class)
    public BaseResponse userTokenExceptionHandler(HttpServletResponse response, LoginException ex) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public BaseResponse usernameNotFoundExceptionHandler(HttpServletResponse response, LoginException ex) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(), "sb："+ex.getMessage());
    }

    @ExceptionHandler(BadCredentialsException.class)
    public BaseResponse badCredentialsExceptionHandler(HttpServletResponse response, LoginException ex) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        log.error(ex.getMessage(),ex);
        return new BaseResponse(ex.getStatus(), "sb："+ex.getMessage());
    }

//    @ExceptionHandler(UserInvalidException.class)
//    public BaseResponse userInvalidExceptionHandler(HttpServletResponse response, UserInvalidException ex) {
//        response.setStatus(HttpStatus.UNAUTHORIZED.value());
//        log.error(ex.getMessage(),ex);
//        return new BaseResponse(ex.getStatus(), ex.getMessage());
//    }
//
//    @ExceptionHandler(BusinessException.class)
//    public BaseResponse businessExceptionHandler(HttpServletResponse response, BusinessException ex) {
//        response.setStatus(HttpStatus.METHOD_FAILURE.value());
//        log.info(ex.getMessage(),ex);
//        return new BaseResponse(ex.getStatus(), ex.getMessage());
//    }
}
